#include <IOwebGUI.h>

const char *ssid = "Testing";
const char *password = "testing123";

unsigned long interval;

std::string TXT = "If the system is enabled via the EN point and the LDR voltage is less than 3000 mV, the LED on pin 19 will turn on. \
If the system is enabled via the EN point, the pwm duty cycle on pin 18 will be set proportional to the LDR voltage.";

DigitalInput en = DigitalInput("EN", 100);
AnalogOutput pwm = AnalogOutput("PWM", 18, 100);
DigitalOutput led = DigitalOutput("LED", 19, 100);
AnalogInput ldr = AnalogInput("LDR", 34, 100);
DigitalInput btn = DigitalInput("BTN", 21, INPUT_PULLUP, 100);
IOPoint* points [5] = { &en, &pwm, &led, &ldr, &btn };
IOwebGUI webGUI;

void automation()
{
    unsigned long now = millis();
  
    if (now - interval >= 100)
    {
        if (en.getDigital() == ON)
        {
            led.setDigital(ldr.getAnalog() < 1500);
            int brightness = map(ldr.getAnalog(), 0, 3300, 0, 255);
            pwm.setAnalog(brightness);
        }
        interval = now;
    }
}

void setup() 
{
    Serial.begin(115200);
    WiFi.softAP(ssid, password);
    webGUI.text = TXT;
    webGUI.init(points, 5);
    ldr.m = 0.8058608058608059;
    en.setDigital(ON);
}

void loop() 
{
    automation();
    webGUI.update(points, 5);
}